package com.taa.trigger.listener;

import com.taa.domain.trade.adapter.repository.ITradeRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.AmqpRejectAndDontRequeueException;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author: taa
 * @Desc:
 * @Create: 2025-06-02 14:59
 **/
@Slf4j
@Component
public class StockUpdateCustomer {
    
    @Value("${spring.rabbitmq.topic.stock_update}")
    private String topic;
    @Resource
    private ITradeRepository tradeRepository;
    
    @RabbitListener(queuesToDeclare = @Queue(value = "stock_update"))
    public void listener(List<Long> productIdList) {
        try{
            log.info("监听商品库存更新消息 topic: {} message: {}", topic, productIdList);
            boolean status = tradeRepository.updateStock(productIdList);
            if (!status) {
                throw new AmqpRejectAndDontRequeueException("库存更新失败");
            }
        } catch (Exception e) {
            log.error("监听商品库存更新消息 消息失败 topic: {} message: {}", topic, productIdList, e);
            throw e;
        }
    }
}

